# -*- coding: utf-8 -*-
'''
Created on 2018年10月17日
@author: Administrator
'''
#1.代理设置
import requests
proxies = {
    "http":"http//127.0.0.1:9743",
    "https":"https://127.0.0.1:9743",
}
response = requests.get("https://www.taobao.com",proxies=proxies)
print(response.status_code)
#代理有用户名和密码的情况
import requests
proxies = {
    "http":"http//user:password@127.0.0.1:9743",
}
response = requests.get("https://www.taobao.com",proxies=proxies)
print(response.status_code)
#代理是非http,https，socks代理，需要先使用pip3 install 'requests[socks]'
import requests
proxies = {
    "http":"sock5://127.0.0.1:9743",
    "https":"sock5://127.0.0.1:9743",
}
response = requests.get("https://www.taobao.com",proxies=proxies)
print(response.status_code)

#2.超时设置
import requests
from requests.exceptions import ReadTimeout
try:
    response = requests.get('http://httpbin.org/get',timeout = 0.1)
    print(response.status_code)
except ReadTimeout:
    print('Timeout')
#3.认证设置
import requests
from requests.auth import HTTPBasicAuth
r = requests.get('http://120.27.34.24',auth=HTTPBasicAuth('user','123'))
print(r.status_code)

#4.异常处理
import requests
from requests.exceptions import ReadTimeout,HTTPError,RequestException
try:
    response = requests.get("http://httpbin.org/get",timeout = 0.2)
    print(response.status_code)
except ReadTimeout:
    print('Timeout')
except HTTPError:
    print('Http error')
except RequestException:
    print('Error')